﻿using System;
using Echo.Context.UltiProNet.Contexts;
using Echo.Core.Common;
using Echo.Core.TestAbstraction.WebUITest.MbUnit;
using Echo.Models.UltimateSoftware.WebUITest.MbUnit;
using Echo.Models.UltiProNet;
using MbUnit.Framework;

////*************************************************************************************************************************************
////Created by: Adam Cando
////Date created: 09/18/2013
////Modified by: John Johnson
////Date modified: 05/21/14
////Reason: New rate needed to be updated
////Converted From SuiteTxContrRate (SWAT) Script.
////Original Test Location: http://devsvn/svn/ultipro/trunk/AutomatedTests/FitNesse/fitnesse/FitNesseRoot/SuiteCompliance/SuiteDotNet/SuiteCompanyTaxSetup/SuiteGuiTests/SuiteTxContrRate/
////*************************************************************************************************************************************

namespace Echo.Automation.Compliance.BusinessRules
{
    [TestFixture]
    [Header("Config")]
    [Config(".NET")]
    [Category("Taxes")]
    [Author("Adam Cando", "adam_cando@ultimatesoftware.com")]
    public class SuiteTxContrRate : MbUnitAutomatedTest
    {
        private UltiPro UltiPro;
        private UltiProDatabaseContext Database;
        private const string CompanyCoid = "C0014";

        #region Setup Methods
        [FixtureSetUp]
        public void FixtureSetUp()
        {
            UltiPro = Launch<UltiPro>();
            Database = Init<UltiProDatabaseContext>();
            Database.Company = ConfigDatabase("ULTIPRO_CALENDAR");
            Database.Company.AddRollBack("TaxCRate", "tcrTaxCode IN ('ALSUIER', 'ALESAER', 'PRSUIER', 'PRSPA', 'WISUIER')");
            Database.Company.AddRollBack("ITaxCode", "ctcCoid = 'C0014' AND ctcTaxCode IN ('NJWFDEE')");
            
            UltiPro.Pages.Login.LoginAs("MerrillG", "password", Config.LanguageCode);
            UltiPro.Pages.CompanySelector.ChooseCompanies("CALEN", "C0014");
        }

        [SetUp]
        public void SetUp()
        {
            UltiPro.Menu.SystemConfiguration.Click(link => link.BusinessRules);
            UltiPro.Pages.BusinessRulesSummary.BusinessRulesSummaryGrid.Find(col => col.BusinessRulesNameFilter, FilterType.Is, "Taxes");
            UltiPro.Pages.BusinessRulesSummary.BusinessRulesSummaryGrid.Row(col => col.BusinessRuleName, "Taxes").Click(col => col.BusinessRulesLink);
        }
        #endregion

        #region Test Scenarios
        [Test]
        public void Active_linked_codes_appear_below_the_SUI_code()
        {
            NavigateToTax("GASUIER");

            var GASUIER = UltiPro.Pages.TaxCodeContributionRate.TaxContribution("GASUIER");
            GASUIER.Is(page => page.TaxCodeLabel, "innerHTML", "GASUIER");
            GASUIER.Is(page => page.ContributionRateTextbox, "value", "6.0000 %");
            GASUIER.NotEnabled(page => page.UseSystemRateCheckbox);

            var GAAATER = UltiPro.Pages.TaxCodeContributionRate.TaxContribution("GAAATER");
            GAAATER.Like(page => page.TaxCodeLabel, "innerHTML", "GAAATER");
            GAAATER.Is(page => page.ContributionRateTextbox, "value", "6.0000 %");
            GAAATER.Enabled(page => page.UseSystemRateCheckbox);
        }

        [Test]
        public void Change_tax_code_to_a_defferent_rate_for_the_future()
        {
            NavigateToTax("WISUIER");
            var WISUIER = UltiPro.Pages.TaxCodeContributionRate.TaxContribution("WISUIER");
            WISUIER.Is(page => page.ContributionRateTextbox, "value", "4.0000 %");

            WISUIER.Set(control => control.ContributionRateTextbox, "4.400");
            WISUIER.Blur(control => control.ContributionRateTextbox);
            UltiPro.Pages.TaxCodeContributionRate.EffectiveDatePicker.Set(DateTime.Today.AddYears(1));
            UltiPro.Pages.TaxCodeContributionRate.Set(page => page.RateChangeReasonDropDown, "RATE", SelectOption.Value);
            UltiPro.Pages.TaxCodeContributionRate.ButtonBar.Click(btnbar => btnbar.Save);

            SetUp();
            NavigateToTax("WISUIER");
            WISUIER.Is(page => page.ContributionRateTextbox, "value", "4.0000 %");
            UltiPro.Pages.TaxCodeContributionRate.Is(page => page.TotalEffectiveRateValue, "innerHTML", "4.0000 %");
        }

        [Test]
        public void Contribution_rate_modifications()
        {
            NavigateToTax("ILSUIER");
            var ILSUIER = UltiPro.Pages.TaxCodeContributionRate.TaxContribution("ILSUIER");
            ILSUIER.Is(page => page.ContributionRateTextbox, "value", "3.0000 %");
            ILSUIER.NotEnabled(page => page.UseSystemRateCheckbox);
            ILSUIER.NotSelected(page => page.UseSystemRateCheckbox);

            var ILFBRER = UltiPro.Pages.TaxCodeContributionRate.TaxContribution("ILFBRER");
            ILFBRER.Is(page => page.ContributionRateTextbox, "value", "0.4000 %");
            ILFBRER.Enabled(page => page.UseSystemRateCheckbox);
            ILFBRER.NotSelected(page => page.UseSystemRateCheckbox);

            UltiPro.Pages.TaxCodeContributionRate.Is(page => page.TotalEmployerRateValue, "innerHTML", "3.4000 %");
            UltiPro.Pages.TaxCodeContributionRate.Is(page => page.TotalEmployeeRateValue, "innerHTML", "0.0000 %");
            UltiPro.Pages.TaxCodeContributionRate.Is(page => page.TotalEffectiveRateValue, "innerHTML", "3.4000 %");

            ILSUIER.Set(col => col.ContributionRateTextbox, "4");
            ILSUIER.Blur(col => col.ContributionRateTextbox);

            UltiPro.Pages.TaxCodeContributionRate.Is(page => page.TotalEmployerRateValue, "innerHTML", "4.4000 %");
            UltiPro.Pages.TaxCodeContributionRate.Is(page => page.TotalEmployeeRateValue, "innerHTML", "0.0000 %");
            UltiPro.Pages.TaxCodeContributionRate.Is(page => page.TotalEffectiveRateValue, "innerHTML", "4.4000 %");
        }

        [Test]
        public void Linked_code_is_inactive()
        {
            NavigateToTax("TNSUIER");

            var TNSUIER = UltiPro.Pages.TaxCodeContributionRate.TaxContribution("TNSUIER");
            TNSUIER.Is(page => page.ContributionRateTextbox, "value", "2.5000 %");
            TNSUIER.NotEnabled(page => page.UseSystemRateCheckbox);
            TNSUIER.NotSelected(page => page.UseSystemRateCheckbox);
        }

        [Test]
        public void Linked_tax_code_is_in_the_company_table()
        {
            NavigateToTax("NJSUIER");

            var NJSUIER = UltiPro.Pages.TaxCodeContributionRate.TaxContribution("NJSUIER");
            NJSUIER.Is(page => page.ContributionRateTextbox, "value", "3.0000 %");
            NJSUIER.NotEnabled(page => page.UseSystemRateCheckbox);

            var NJWFDER = UltiPro.Pages.TaxCodeContributionRate.TaxContribution("NJWFDER");
            NJWFDER.Is(page => page.ContributionRateTextbox, "value", "4.0000 %");
            NJWFDER.Enabled(page => page.UseSystemRateCheckbox);
            NJWFDER.NotSelected(page => page.UseSystemRateCheckbox);

            var NJSUIEE = UltiPro.Pages.TaxCodeContributionRate.TaxContribution("NJSUIEE");
            NJSUIEE.Is(page => page.ContributionRateTextbox, "value", "4.0000 %");
            NJSUIEE.NotEnabled(page => page.UseSystemRateCheckbox);
            NJSUIEE.NotSelected(page => page.UseSystemRateCheckbox);

            var NJWFDEE = UltiPro.Pages.TaxCodeContributionRate.TaxContribution("NJWFDEE");
            NJWFDEE.Is(page => page.ContributionRateTextbox, "value", "5.0000 %");
            NJWFDEE.NotEnabled(page => page.UseSystemRateCheckbox);
            NJWFDEE.NotSelected(page => page.UseSystemRateCheckbox);

            UltiPro.Pages.TaxCodeContributionRate.Is(page => page.TotalEmployerRateValue, "innerHTML", "7.0000 %");
            UltiPro.Pages.TaxCodeContributionRate.Is(page => page.TotalEmployeeRateValue, "innerHTML", "9.0000 %");
            UltiPro.Pages.TaxCodeContributionRate.Is(page => page.TotalEffectiveRateValue, "innerHTML", "16.0000 %");

            Database.Company.Execute("DELETE FROM ITaxCode WHERE ctcCoid = 'c0014' AND ctcTaxCode = 'NJWFDEE'");

            SetUp();
            NavigateToTax("NJSUIER");

            UltiPro.Pages.TaxCodeContributionRate.TaxContribution("NJWFDEE").NotExists(col => col.TaxCodeLabel);
            UltiPro.Pages.TaxCodeContributionRate.Is(page => page.TotalEmployerRateValue, "innerHTML", "7.0000 %");
            UltiPro.Pages.TaxCodeContributionRate.Is(page => page.TotalEmployeeRateValue, "innerHTML", "4.0000 %");
            UltiPro.Pages.TaxCodeContributionRate.Is(page => page.TotalEffectiveRateValue, "innerHTML", "11.0000 %");
        }

        [Test]
        public void State_defined_flag_is_no_and_there_is_a_system_rate()
        {
            NavigateToTax("PRSUIER");

            var PRSUIER = UltiPro.Pages.TaxCodeContributionRate.TaxContribution("PRSUIER");
            PRSUIER.Is(control => control.ContributionRateTextbox, "value", "0.2700 %");
            PRSUIER.NotEnabled(control => control.UseSystemRateCheckbox);
            PRSUIER.NotSelected(control => control.UseSystemRateCheckbox);

            var PRSPA = UltiPro.Pages.TaxCodeContributionRate.TaxContribution("PRSPA");
            PRSPA.Is(control => control.ContributionRateTextbox, "value", "0.3300 %");
            PRSPA.Enabled(control => control.UseSystemRateCheckbox);
            PRSPA.NotSelected(control => control.UseSystemRateCheckbox);

            UltiPro.Pages.TaxCodeContributionRate.NotVisible(page => page.EffectiveDateLabel);
            UltiPro.Pages.TaxCodeContributionRate.EffectiveDatePicker.NotVisibleOrNotExists();
            UltiPro.Pages.TaxCodeContributionRate.NotVisible(page => page.RateChangeReasonLabel);
            UltiPro.Pages.TaxCodeContributionRate.NotVisible(page => page.RateChangeReasonDropDown);

            PRSPA.Click(control => control.UseSystemRateCheckbox);

            UltiPro.Pages.TaxCodeContributionRate.Visible(page => page.EffectiveDateLabel);
            UltiPro.Pages.TaxCodeContributionRate.EffectiveDatePicker.Exists();
            UltiPro.Pages.TaxCodeContributionRate.Visible(page => page.RateChangeReasonLabel);
            UltiPro.Pages.TaxCodeContributionRate.Visible(page => page.RateChangeReasonDropDown);
            UltiPro.Pages.TaxCodeContributionRate.Is(page => page.RateChangeReasonDropDown, "required", "true");
            UltiPro.Pages.TaxCodeContributionRate.EffectiveDatePicker.Set(DateTime.Today);
            UltiPro.Pages.TaxCodeContributionRate.Set(page => page.RateChangeReasonDropDown, "RATE", SelectOption.Value);
            UltiPro.Pages.TaxCodeContributionRate.ButtonBar.Click(btnbar => btnbar.Save);

            SetUp();
            NavigateToTax("PRSUIER");

            PRSUIER.Is(control => control.ContributionRateTextbox, "value", "0.2700 %");
            PRSUIER.NotEnabled(control => control.UseSystemRateCheckbox);
            PRSUIER.NotSelected(control => control.UseSystemRateCheckbox);
            // value below was modified from .3 to 1.0 because change in system
            PRSPA.Is(control => control.ContributionRateTextbox, "value", "1.0000 %");
            PRSPA.Enabled(control => control.UseSystemRateCheckbox);
            PRSPA.Selected(control => control.UseSystemRateCheckbox);

            UltiPro.Pages.TaxCodeContributionRate.Is(page => page.TotalEmployerRateValue, "innerHTML", "1.2700 %");
            UltiPro.Pages.TaxCodeContributionRate.Is(page => page.TotalEmployeeRateValue, "innerHTML", "0.0000 %");
            UltiPro.Pages.TaxCodeContributionRate.Is(page => page.TotalEffectiveRateValue, "innerHTML", "1.2700 %");
        }

        [Test]
        public void State_defined_flag_is_yes_and_there_is_a_system_rate()
        {
            NavigateToTax("TXSUIER");

            var TXSUIER = UltiPro.Pages.TaxCodeContributionRate.TaxContribution("TXSUIER");
            TXSUIER.Is(page => page.ContributionRateTextbox, "value", "5.0000 %");
            TXSUIER.NotEnabled(page => page.UseSystemRateCheckbox);
            TXSUIER.NotSelected(page => page.UseSystemRateCheckbox);

            var TXETAER = UltiPro.Pages.TaxCodeContributionRate.TaxContribution("TXETAER");
            TXETAER.Enabled(page => page.UseSystemRateCheckbox);
            TXETAER.Selected(page => page.UseSystemRateCheckbox);
            TXETAER.NotEnabled(page => page.ContributionRateTextbox);
            TXETAER.Is(page => page.ContributionRateTextbox, "value", "0.1000 %");
        }

        [Test]
        public void Validate_ALSUIER_with_linking()
        {
            NavigateToTax("ALSUIER");

            var ALSUIER = UltiPro.Pages.TaxCodeContributionRate.TaxContribution("ALSUIER");
            ALSUIER.Is(page => page.ContributionRateTextbox, "value", "0.2200 %");

            var ALESAER = UltiPro.Pages.TaxCodeContributionRate.TaxContribution("ALESAER");
            ALESAER.Is(page => page.ContributionRateTextbox, "value", "0.3300 %");

            ALSUIER.Set(control => control.ContributionRateTextbox, "0.3400");
            ALSUIER.Blur(control => control.ContributionRateTextbox);
            ALESAER.Set(control => control.ContributionRateTextbox, "0.4400");
            ALESAER.Blur(control => control.ContributionRateTextbox);
            UltiPro.Pages.TaxCodeContributionRate.EffectiveDatePicker.Set(DateTime.Today.AddYears(1));
            UltiPro.Pages.TaxCodeContributionRate.Set(page => page.RateChangeReasonDropDown, "RATE", SelectOption.Value);
            UltiPro.Pages.TaxCodeContributionRate.ButtonBar.Click(btnbar => btnbar.Save);

            SetUp();
            NavigateToTax("ALSUIER");
            UltiPro.Pages.TaxCodeContributionRate.Is(page => page.TotalEmployerRateValue, "innerHTML", "0.5500 %");
            UltiPro.Pages.TaxCodeContributionRate.Is(page => page.TotalEmployeeRateValue, "innerHTML", "0.0000 %");
            UltiPro.Pages.TaxCodeContributionRate.Is(page => page.TotalEffectiveRateValue, "innerHTML", "0.5500 %");
        }

        [Test]
        public void Validate_ER_is_above_EE_and_EE_is_disabled()
        {
            NavigateToTax("AKSUIER");
            UltiPro.Pages.TaxCodeContributionRate.TaxContribution("AKSUIER").Is(page => page.ContributionRateTextbox, "value", "0.0500 %");
            UltiPro.Pages.TaxCodeContributionRate.TaxContribution("AKSUIEE").Is(col => col.ContributionRateTextbox, "value", "0.6200 %");
            UltiPro.Pages.TaxCodeContributionRate.TaxContribution("AKSUIEE").NotEnabled(col => col.UseSystemRateCheckbox);
        }

        [Test]
        [Header("Tax Code")]
        [Row("AKSUIEE", "AK")]
        [Row("NJWFDER", "NJ")]
        public void Validate_information_message_for_employer_identification_number(string taxCode, string abriviation)
        {
            NavigateToTax(taxCode);
            UltiPro.Pages.TaxCodeContributionRate.InformationMessages.HasOnly(string.Format("The employer identification number and contribution rate for this tax must be set up on the State Unemployment Insurance (SUI) tax, {0} Unemployment Employer.", abriviation));
        }
        #endregion

        #region Teardown Methods
        [TearDown]
        public void TearDown()
        {
            Database.Company.RollBackDatabase();
        }

        [FixtureTearDown]
        public void FixtureTearDown()
        {
            UltiPro.Browser.Quit();
        }
        #endregion

        #region Helper Methods
        private void NavigateToTax(string taxCode)
        {
            UltiPro.Pages.BusinessRuleDetail.BusinessRuleDetailGrid.AddFilter(col => col.CompanyFilter, FilterType.Is, CompanyCoid);
            UltiPro.Pages.BusinessRuleDetail.BusinessRuleDetailGrid.Find(col => col.CodeFilter, FilterType.Is, taxCode);
            UltiPro.Pages.BusinessRuleDetail.BusinessRuleDetailGrid.Row(col => col.Code, taxCode).Click(col => col.Code);
        }
        #endregion
    }
}
